package com.example.config;

import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class LoginInterceptor implements HandlerInterceptor {
    /**
     * 自定义拦截方法 返回true时表示可以访问后端接口，返回false时表示无权访问后端接口
     * @param request
     * @param response
     * @param handler
     * @return
     * @throws Exception
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
            throws Exception {
        HttpSession session = request.getSession(false);
        //判断session是否有值
        if(session != null &&
             session.getAttribute(AppFinal.USERINFO_SESSIONKEY) != null){
            //表示用户已经登录
            return true;
        }
        return false;
    }
}
